﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLogic
{
    public class ChiTietQuyen
    {
        public DataTable SelectByKey(string tennhomquyen)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            try
            {
                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_ChiTietQuyen_SelectByTenNhomQuyen";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenNhomQuyen", SqlDbType.NVarChar, 50).Value = tennhomquyen;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                adapter.Fill(ds, "ChiTietQuyen");
                dt = ds.Tables["ChiTietQuyen"];
            }
            catch
            {
                dt = null;
            }
            finally
            {
                con.Close();
            }
            return dt;
        }
        public int Insert(Entities.ChiTietQuyen ctq)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            int i = 0;
            try
            {
                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_ChiTietQuyen_Insert";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenNhomQuyen", SqlDbType.NVarChar, 200).Value = ctq.TenNhomQuyen;
                cmd.Parameters.Add("@TenForm", SqlDbType.NVarChar, 200).Value = ctq.TenForm;
                cmd.Parameters.Add("@QuyenThem", SqlDbType.Bit).Value = ctq.QuyenThem;
                cmd.Parameters.Add("@QuyenSua", SqlDbType.NVarChar, 200).Value = ctq.QuyenSua;
                cmd.Parameters.Add("@QuyenXoa", SqlDbType.NVarChar, 20).Value = ctq.QuyenXoa;
                cmd.Parameters.Add("@QuyenXem", SqlDbType.Bit).Value = ctq.QuyenXem;


                i = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {

                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }
        public bool Update(Entities.ChiTietQuyen[] actq,Entities.TaiKhoan TK)
        {

            SqlConnection con = DataAccess.Connection.OpenConnection();
            SqlTransaction tran;
            bool i = false;
            try
            {

                
                tran = con.BeginTransaction("Trans");
                SqlCommand cmd = new SqlCommand();
                cmd.Transaction = tran;
                cmd.CommandText = "sp_ChiTietQuyen_Update";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                try
                {
                    foreach (Entities.ChiTietQuyen ctq in actq)
                    {
                        cmd.Parameters.Clear();

                        cmd.Parameters.Add("@TenNhomQuyen", SqlDbType.NVarChar, 200).Value = ctq.TenNhomQuyen;
                        cmd.Parameters.Add("@TenForm", SqlDbType.NVarChar, 200).Value = ctq.TenForm;
                        cmd.Parameters.Add("@QuyenThem", SqlDbType.Bit).Value = ctq.QuyenThem;
                        cmd.Parameters.Add("@QuyenSua", SqlDbType.NVarChar, 200).Value = ctq.QuyenSua;
                        cmd.Parameters.Add("@QuyenXoa", SqlDbType.NVarChar, 20).Value = ctq.QuyenXoa;
                        cmd.Parameters.Add("@QuyenXem", SqlDbType.Bit).Value = ctq.QuyenXem;

                        cmd.ExecuteNonQuery();
                    }
                    tran.Commit();
                    i = true;
                    LuuVietHeThong.LuuViet("Update", TK.MaNhanVien, "Sửa Quyền: " + actq[0].TenNhomQuyen, TK.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
                }
                catch (Exception)
                {

                    tran.Rollback("trans");
                }


            }
            catch (Exception)
            {
            }
            finally
            {
                con.Close();
            }
            return i;

        }
    }
}
